package org.csu.onlineedubackend.controller;

import org.csu.onlineedubackend.result.CommonResponse;
import org.csu.onlineedubackend.service.ForumLikeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/api/forum/like")
public class ForumLikeController {
    @Autowired
    private ForumLikeService forumLikeService;

    /**
     * 點贊
     */
    @PostMapping
    public CommonResponse<Boolean> like(@RequestParam Integer userId, 
                                      @RequestParam(required = false) Integer postId, 
                                      @RequestParam(required = false) Integer commentId) {
        try {
            boolean result = forumLikeService.like(userId, postId, commentId);
            return CommonResponse.createForSuccess(result);
        } catch (Exception e) {
            return CommonResponse.createForError("點贊失敗：" + e.getMessage());
        }
    }

    /**
     * 取消點贊
     */
    @DeleteMapping
    public CommonResponse<Boolean> unlike(@RequestParam Integer userId, 
                                        @RequestParam(required = false) Integer postId, 
                                        @RequestParam(required = false) Integer commentId) {
        try {
            boolean result = forumLikeService.unlike(userId, postId, commentId);
            return CommonResponse.createForSuccess(result);
        } catch (Exception e) {
            return CommonResponse.createForError("取消點贊失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢是否點贊
     */
    @GetMapping("/isLiked")
    public CommonResponse<Boolean> isLiked(@RequestParam Integer userId, 
                                         @RequestParam(required = false) Integer postId, 
                                         @RequestParam(required = false) Integer commentId) {
        try {
            boolean result = forumLikeService.isLiked(userId, postId, commentId);
            return CommonResponse.createForSuccess(result);
        } catch (Exception e) {
            return CommonResponse.createForError("查詢點贊狀態失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢帖子點贊數
     */
    @GetMapping("/postCount/{postId}")
    public CommonResponse<Integer> getPostLikeCount(@PathVariable Integer postId) {
        try {
            int count = forumLikeService.getPostLikeCount(postId);
            return CommonResponse.createForSuccess(count);
        } catch (Exception e) {
            return CommonResponse.createForError("查詢帖子點贊數失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢評論點贊數
     */
    @GetMapping("/commentCount/{commentId}")
    public CommonResponse<Integer> getCommentLikeCount(@PathVariable Integer commentId) {
        try {
            int count = forumLikeService.getCommentLikeCount(commentId);
            return CommonResponse.createForSuccess(count);
        } catch (Exception e) {
            return CommonResponse.createForError("查詢評論點贊數失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢用戶收到的總點贊數
     */
    @GetMapping("/receivedCount/{userId}")
    public CommonResponse<Integer> getUserReceivedLikeCount(@PathVariable Integer userId) {
        try {
            int count = forumLikeService.getUserReceivedLikeCount(userId);
            return CommonResponse.createForSuccess(count);
        } catch (Exception e) {
            return CommonResponse.createForError("查詢用戶收到的點贊數失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢用戶點贊過的帖子數量
     */
    @GetMapping("/givenCount/{userId}")
    public CommonResponse<Integer> getUserGivenLikeCount(@PathVariable Integer userId) {
        try {
            int count = forumLikeService.getUserGivenLikeCount(userId);
            return CommonResponse.createForSuccess(count);
        } catch (Exception e) {
            return CommonResponse.createForError("查詢用戶點贊過的帖子數失敗：" + e.getMessage());
        }
    }

    /**
     * 查詢用戶點贊過的帖子列表
     */
    @GetMapping("/user/{userId}")
    public CommonResponse<java.util.List<org.csu.onlineedubackend.dto.ForumLikeDTO>> getLikesByUser(@PathVariable Integer userId) {
        try {
            java.util.List<org.csu.onlineedubackend.dto.ForumLikeDTO> likes = forumLikeService.getLikesByUser(userId);
            return CommonResponse.createForSuccess(likes);
        } catch (Exception e) {
            return CommonResponse.createForError("查詢點贊列表失敗：" + e.getMessage());
        }
    }
} 